package com.vates.curso.dao.imp;

import java.io.Serializable;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate3.HibernateTemplate;

import com.vates.curso.dao.GenericDao;

/**
 * This class serves as the Base class for all other DAOs - namely to hold
 * common CRUD methods that they might all use. You should only need to extend
 * this class when your require custom CRUD logic.
 * <p/>
 * <p>
 * To register this class in your Spring context file, use the following XML.
 * 
 * <pre>
 *      &lt;bean id="fooDao" class="org.appfuse.dao.hibernate.GenericDaoHibernate"&gt;
 *          &lt;constructor-arg value="org.appfuse.model.Foo"/&gt;
 *      &lt;/bean&gt;
 * </pre>
 * 
 * @author <a href="mailto:bwnoll@gmail.com">Bryan Noll</a>
 * @param <T>
 *            a type variable
 * @param <PK>
 *            the primary key for that type
 */
public abstract class GenericDaoHibernate<T, PK extends Serializable>
		implements GenericDao<T, PK> {
	/** The hibernate template. */
	@Autowired
	private HibernateTemplate hibernateTemplate;

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.vates.practicofinal.dao.hibernate.BaseDao#save(java.lang.Object)
	 */
	public void save(T entity) {
		hibernateTemplate.saveOrUpdate(entity);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.vates.practicofinal.dao.hibernate.BaseDao#delete(java.lang.Object)
	 */
	public void delete(T entity) {
		hibernateTemplate.delete(entity);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.vates.practicofinal.dao.hibernate.BaseDao#update(java.lang.Object)
	 */
	public void update(T entity) {
		hibernateTemplate.saveOrUpdate(entity);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.vates.practicofinal.dao.hibernate.BaseDao#get(java.io.Serializable)
	 */
	public T get(PK id) {
		return hibernateTemplate.get(getRelatedEntityClass(), id);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.vates.practicofinal.dao.hibernate.BaseDao#getAll()
	 */
	public List<T> getAll() {
		return hibernateTemplate.loadAll(getRelatedEntityClass());
	}

	public List<T> executeSQLQuery(String sqlQuery) {
		return hibernateTemplate.find(sqlQuery);
	}
}
